from django.shortcuts import render
from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt

import json
import pymysql

# Create your views here.
# request第一个参数必不可少，将浏览器请求的对象传过来

@csrf_exempt
def register(request):
    USERNAME = ''
    PASSORD = ''
    EMAIL = ''
    AGE = ''
    GENDER = ''
    canregister = True
    # 1.注册指令处理
    if request.method == 'POST':
        print("注册信息：")
        print("username:", request.POST.get('username'))
        print("password:", request.POST.get('password'))
        print("email:", request.POST.get('email'))
        print("gender:", request.POST.get('gender'))
        print("age:", request.POST.get('age'))
        USERNAME = request.POST.get('username')
        PASSORD = request.POST.get('password')
        EMAIL = request.POST.get('email')
        AGE = request.POST.get('age')
        GENDER = request.POST.get('gender')
    elif request.method == 'GET':
        print("注册信息：")
        print("username:", request.GET.get('username'))
        print("password:", request.GET.get('password'))
        print("email:", request.GET.get('email'))
        print("gender:", request.GET.get('gender'))
        print("age:", request.GET.get('age'))
        USERNAME = request.GET.get('username')
        PASSORD = request.GET.get('password')
        EMAIL = request.GET.get('email')
        AGE = request.GET.get('age')
        GENDER = request.GET.get('gender')
    # a.打开数据库连接
    database = pymysql.connect(host='localhost'
                               , user='root'
                               , password='000000'
                               , port=3306
                               , database='WHXD')

    # b.使用cursor()方法创建一个游标对象cur
    cur = database.cursor()
    print("[注册]已经连接到WHXD数据库！")

    sql = "SELECT USER_ID FROM USERS WHERE USER_ID='%s'" % USERNAME  # 查询该用户名是否存在
    print(sql)
    # c.使用execute()方法执行SQL语句
    cur.execute(sql)
    result = cur.fetchall()
    if len(result) == 0:
        # 该用户名不存在——可以注册
        sql = "INSERT INTO USERS VALUES('%s','%s','%s',%s,'%s')" % (USERNAME, PASSORD, EMAIL, AGE, GENDER)
        print(sql)
        cur.execute(sql)
        database.commit()

        # 给该新注册用户创建一个关注表
        sql = '''
              CREATE TABLE IF NOT EXISTS %s_FOLLOW
              (
                    ID INT NOT NULL AUTO_INCREMENT, 
                    FOLLOW_ID VARCHAR(32) NOT NULL,
                    PRIMARY KEY(ID),
                    FOREIGN KEY(FOLLOW_ID) REFERENCES USERS(USER_ID) ON DELETE CASCADE ON UPDATE CASCADE
              )
              ''' % (USERNAME.upper())
        print(sql)
        cur.execute(sql)

        # 给该新注册用户创建一个粉丝表
        sql = '''
              CREATE TABLE IF NOT EXISTS %s_FANS
              (
                    ID INT NOT NULL AUTO_INCREMENT, 
                    FANS_ID VARCHAR(32) NOT NULL,
                    PRIMARY KEY(ID),
                    FOREIGN KEY(FANS_ID) REFERENCES USERS(USER_ID) ON DELETE CASCADE ON UPDATE CASCADE
              )
              ''' % (USERNAME.upper())
        print(sql)
        cur.execute(sql)
        print("已经成功注册该用户！")
    else:
        # 该用户名存在——不能注册
        print("该账户已经存在！")
        canregister = False

    # d.关闭数据库连接
    database.close()
    print("[注册]已经关闭WHXD数据库连接！")

    if canregister == True:
        return HttpResponse("OK")
    else:
        return HttpResponse("NO")
# --------------------------已完成-分割线----------------------#

@csrf_exempt
def login(request):
    USERNAME = ''
    PASSORD = ''
    # 2.登录指令处理
    if request.method == 'POST':
        print("登陆信息：")
        print("username:", request.POST.get('username'))
        print("password:", request.POST.get('password'))
        USERNAME = request.POST.get('username')
        PASSORD = request.POST.get('password')
    elif request.method == 'GET':
        print("登陆信息：")
        print("username:", request.GET.get('username'))
        print("password:", request.GET.get('password'))
        USERNAME = request.GET.get('username')
        PASSORD = request.GET.get('password')
    # a.打开数据库连接
    database = pymysql.connect(host='localhost'
                               , user='root'
                               , password='000000'
                               , port=3306
                               , database='WHXD')

    # b.使用cursor()方法创建一个游标对象cur
    cur = database.cursor()
    print("[登录]已经连接到WHXD数据库！")

    sql = "SELECT USER_ID,PASSWORD,EMAIL,GENDER,AGE FROM USERS WHERE USER_ID='%s'" % USERNAME  # 查询该用户名是否存在
    print(sql)
    # c.使用execute()方法执行SQL语句
    cur.execute(sql)
    result = cur.fetchall()
    # d.关闭数据库连接
    database.close()
    print("[登录]已经关闭WHXD数据库连接！")

    if len(result) == 0:
        print("账户不存在，请注册！")
        return HttpResponse("NOT")
    else:
        if PASSORD == result[0][1]:
            print("密码正确，登录成功！")
            backinfo = 'OK' \
                       + ',' + str(result[0][0]) \
                       + ',' + str(result[0][1]) \
                       + ',' + str(result[0][2]) \
                       + ',' + str(result[0][4]) \
                       + ',' + str(result[0][3])
            return HttpResponse(backinfo)
        else:
            print("密码错误，登录失败！")
            return HttpResponse("NO")
# --------------------------已完成-分割线----------------------#

@csrf_exempt
def destory(request):
    USERNAME = ''
    # 3.销毁指令处理
    if request.method == 'POST':
        print("被销毁账号：")
        print("username:", request.POST.get('username'))
        USERNAME = request.POST.get('username')
    elif request.method == 'GET':
        print("被销毁账号：")
        print("username:", request.GET.get('username'))
        USERNAME = request.GET.get('username')

    # a.打开数据库连接
    database = pymysql.connect(host='localhost'
                               , user='root'
                               , password='000000'
                               , port=3306
                               , database='WHXD')

    # b.使用cursor()方法创建一个游标对象cur
    cur = database.cursor()
    print("[销毁]已经连接到WHXD数据库！")

    sql = '''
          DELETE FROM USERS WHERE USER_ID='%s'
          '''%USERNAME
    print(sql)
    # c.使用execute()方法执行SQL语句
    cur.execute(sql)
    database.commit()
    print("1.已经删除该用户信息！")


    sql = '''DROP TABLE IF EXISTS %s_FOLLOW''' % USERNAME
    cur.execute(sql)
    sql = '''DROP TABLE IF EXISTS %s_FANS''' % USERNAME
    cur.execute(sql)
    print("3.已经删除该用户关注和粉丝表！")

    # d.关闭数据库连接
    database.close()
    print("[销毁]已经关闭WHXD数据库连接！")

    return HttpResponse("OK")
# --------------------------已完成-分割线----------------------#

@csrf_exempt
def read(request):
    USERNAME = ''
    CONTENT_ID = ''
    USER_ID = ''
    isfollow = '0'
    isagree = '0'
    iscollect = '0'
    # 阅读详情指令
    if request.method == 'POST':
        print("[指令]阅读详情")
        USERNAME = request.POST.get('username')
        CONTENT_ID = int(request.POST.get('content_id'))
        USER_ID = request.POST.get('user_id')
    elif request.method == 'GET':
        print("[指令]阅读详情")
        USERNAME = request.GET.get('username')
        CONTENT_ID = int(request.GET.get('content_id'))
        USER_ID = request.GET.get('user_id')
    # a.打开数据库连接
    database = pymysql.connect(host='localhost'
                               , user='root'
                               , password='000000'
                               , port=3306
                               , database='WHXD')

    # b.使用cursor()方法创建一个游标对象cur
    cur = database.cursor()
    print("[阅读指令]已经连接到WHXD数据库！")
    sql = '''
          SELECT READVALUE FROM USER_CONTENTS WHERE CONTENT_ID=%s
          ''' % CONTENT_ID
    cur.execute(sql)
    count = cur.fetchall()
    count = count[0][0] + 1
    sql = '''
            UPDATE USER_CONTENTS SET READVALUE=%s  WHERE CONTENT_ID=%s
          ''' % (count, CONTENT_ID)
    cur.execute(sql)
    database.commit()

    # 查询USERBANE是否关注了USER_ID
    sql = '''SELECT FOLLOW_ID FROM %s_FOLLOW WHERE FOLLOW_ID='%s'
          ''' % (USERNAME, USER_ID)
    print(sql)
    cur.execute(sql)
    result = cur.fetchall()
    print(result)
    if len(result) == 0:
        isfollow = '0'
    else:
        isfollow = '1'

    # 查询USERNAME是否点赞了CONTENT_ID
    sql = '''SELECT CONTENT_ID,USER_ID FROM AGREE WHERE CONTENT_ID=%s AND USER_ID='%s'
          ''' % (CONTENT_ID, USERNAME)
    print(sql)
    cur.execute(sql)
    result = cur.fetchall()
    if len(result) == 0:
        isagree = '0'
    else:
        isagree = '1'

    # 查询USERNAME是否收藏了CONTENT_ID
    sql = '''SELECT CONTENT_ID,USER_ID FROM COLLECT WHERE CONTENT_ID=%s AND USER_ID='%s'
          ''' % (CONTENT_ID, USERNAME)
    print(sql)
    cur.execute(sql)
    result = cur.fetchall()
    if len(result) == 0:
        iscollect = '0'
    else:
        iscollect = '1'

    # 查询
    # d.关闭数据库连接
    database.close()
    print("[阅读指令]已经关闭WHXD数据库连接！")

    return HttpResponse('ok:' + isfollow + isagree + iscollect)
# --------------------------已完成-分割线----------------------#

@csrf_exempt
def worlddt(request):
    # 4.全站动态列表请求指令处理
    if request.method == 'POST':
        print("[指令]全站动态列表")
    elif request.method == 'GET':
        print("[指令]全站动态列表")
    # a.打开数据库连接
    database = pymysql.connect(host='localhost'
                               , user='root'
                               , password='000000'
                               , port=3306
                               , database='WHXD')

    # b.使用cursor()方法创建一个游标对象cur
    cur = database.cursor()
    print("[请求全站动态列表]已经连接到WHXD数据库！")
    sql = '''
            SELECT CONTENT_ID,TYPE,DATE,READVALUE,DETAIL,USER_ID,FIELD_ID FROM USER_CONTENTS
            WHERE TYPE='动态' ORDER BY DATE DESC
          '''
    cur.execute(sql)
    result = cur.fetchall()
    # d.关闭数据库连接
    database.close()
    print("[请求全站动态列表]已经关闭WHXD数据库连接！")

    result_len = len(result)
    if result_len == 0:
        print("全站动态列表查询为空！")
        return HttpResponse("NULL")
    else:
        backjson = {}
        table_fiels = ['CONTENT_ID', 'TYPE', 'DATE', 'READVALUE', 'DETAIL', 'USER_ID', 'FIELD_ID']
        backjson['sum'] = result_len
        for i in range(0, result_len):
            record = {}
            for j in range(0, len(result[i])):
                if j == 2:
                    record[table_fiels[j]] = result[i][j].strftime('%Y-%m-%d %H:%M:%S')
                else:
                    record[table_fiels[j]] = result[i][j]
            backjson[str(i)] = record
        print("backjson is:", backjson)

    return HttpResponse(json.dumps(backjson, ensure_ascii=False))
# --------------------------已完成-分割线----------------------#

@csrf_exempt
def worldxm(request):
    # 5.全站项目列表指令处理
    if request.method == 'POST':
        print("[指令]全站项目列表")
    elif request.method == 'GET':
        print("[指令]全站项目列表")
    # a.打开数据库连接
    database = pymysql.connect(host='localhost'
                               , user='root'
                               , password='000000'
                               , port=3306
                               , database='WHXD')

    # b.使用cursor()方法创建一个游标对象cur
    cur = database.cursor()
    print("[请求全站项目列表]已经连接到WHXD数据库！")
    sql = '''
            SELECT CONTENT_ID,TYPE,DATE,READVALUE,DETAIL,USER_ID,FIELD_ID FROM USER_CONTENTS
            WHERE TYPE='项目' ORDER BY DATE DESC
          '''
    cur.execute(sql)
    result = cur.fetchall()
    # d.关闭数据库连接
    database.close()
    print("[请求全站项目列表]已经关闭WHXD数据库连接！")

    result_len = len(result)
    if result_len == 0:
        print("全站项目列表查询为空！")
        return HttpResponse("NULL")
    else:
        backjson = {}
        table_fiels = ['CONTENT_ID', 'TYPE', 'DATE', 'READVALUE', 'DETAIL', 'USER_ID', 'FIELD_ID']
        backjson['sum'] = result_len
        for i in range(0, result_len):
            record = {}
            for j in range(0, len(result[i])):
                if j == 2:
                    record[table_fiels[j]] = result[i][j].strftime('%Y-%m-%d %H:%M:%S')
                else:
                    record[table_fiels[j]] = result[i][j]
            backjson[str(i)] = record
        print("backjson is:", backjson)

    return HttpResponse(json.dumps(backjson, ensure_ascii=False))
# --------------------------已完成-分割线----------------------#

@csrf_exempt
def worldwz(request):
    # 6.全站文章列表指令处理
    if request.method == 'POST':
        print("[指令]全站文章列表")
    elif request.method == 'GET':
        print("[指令]全站文章列表")
    # a.打开数据库连接
    database = pymysql.connect(host='localhost'
                               , user='root'
                               , password='000000'
                               , port=3306
                               , database='WHXD')

    # b.使用cursor()方法创建一个游标对象cur
    cur = database.cursor()
    print("[请求全站文章列表]已经连接到WHXD数据库！")
    sql = '''
            SELECT CONTENT_ID,TYPE,DATE,READVALUE,DETAIL,USER_ID,FIELD_ID FROM USER_CONTENTS
            WHERE TYPE='文章' ORDER BY DATE DESC
          '''
    cur.execute(sql)
    result = cur.fetchall()
    # d.关闭数据库连接
    database.close()
    print("[请求全站文章列表]已经关闭WHXD数据库连接！")

    result_len = len(result)
    if result_len == 0:
        print("全站文章列表查询为空！")
        return HttpResponse("NULL")
    else:
        backjson = {}
        table_fiels = ['CONTENT_ID', 'TYPE', 'DATE', 'READVALUE', 'DETAIL', 'USER_ID', 'FIELD_ID']
        backjson['sum'] = result_len
        for i in range(0, result_len):
            record = {}
            for j in range(0, len(result[i])):
                if j == 2:
                    record[table_fiels[j]] = result[i][j].strftime('%Y-%m-%d %H:%M:%S')
                else:
                    record[table_fiels[j]] = result[i][j]
            backjson[str(i)] = record
        print("backjson is:", backjson)

    return HttpResponse(json.dumps(backjson, ensure_ascii=False))
# --------------------------已完成-分割线----------------------#

@csrf_exempt
def worldfield(request):
    FIELD_ID = ''
    # 请求领域数据
    if request.method == 'POST':
        print("[指令]领域列表")
        FIELD_ID = request.POST.get('field_id')
    elif request.method == 'GET':
        print("[指令]领域列表")
        FIELD_ID = request.GET.get('field_id')

    # a.打开数据库连接
    database = pymysql.connect(host='localhost'
                               , user='root'
                               , password='000000'
                               , port=3306
                               , database='WHXD')

    # b.使用cursor()方法创建一个游标对象cur
    cur = database.cursor()
    print("[请求领域列表]已经连接到WHXD数据库！")
    sql = '''
            SELECT CONTENT_ID,TYPE,DATE,READVALUE,DETAIL,USER_ID,FIELD_ID FROM USER_CONTENTS
            WHERE FIELD_ID=%s ORDER BY DATE DESC
          ''' % FIELD_ID
    cur.execute(sql)
    result = cur.fetchall()
    # d.关闭数据库连接
    database.close()
    print("[请求领域列表]已经关闭WHXD数据库连接！")

    result_len = len(result)
    if result_len == 0:
        print("领域列表查询为空！")
        return HttpResponse("NULL")
    else:
        backjson = {}
        table_fiels = ['CONTENT_ID', 'TYPE', 'DATE', 'READVALUE', 'DETAIL', 'USER_ID', 'FIELD_ID']
        backjson['sum'] = result_len
        for i in range(0, result_len):
            record = {}
            for j in range(0, len(result[i])):
                if j == 2:
                    record[table_fiels[j]] = result[i][j].strftime('%Y-%m-%d %H:%M:%S')
                else:
                    record[table_fiels[j]] = result[i][j]
            backjson[str(i)] = record
        print("backjson is:", backjson)
    return HttpResponse(json.dumps(backjson, ensure_ascii=False))
# --------------------------已完成-分割线----------------------#

@csrf_exempt
def follow(request):
    # 14.某用户关注他人指令处理
    USERNAME = ''
    USER_ID = ''

    if request.method == 'POST':
        print("[指令]某用户关注他人")
        print("username:", request.POST.get('username'))
        print("USER_ID:", request.POST.get('user_id'))
        USERNAME = request.POST.get('username')
        USER_ID = request.POST.get('user_id')
    elif request.method == 'GET':
        print("[指令]某用户关注他人")
        print("username:", request.GET.get('username'))
        print("USER_ID:", request.GET.get('user_id'))
        USERNAME = request.GET.get('username')
        USER_ID = request.GET.get('user_id')

    # a.打开数据库连接
    database = pymysql.connect(host='localhost'
                               , user='root'
                               , password='000000'
                               , port=3306
                               , database='WHXD')

    # b.使用cursor()方法创建一个游标对象cur
    cur = database.cursor()
    print("[关注他人]已经连接到WHXD数据库！")

    # 向该用户的关注表中写入数据
    sql = '''
          INSERT INTO %s_FOLLOW (FOLLOW_ID) VALUE('%s')
          ''' % (USERNAME, USER_ID)
    cur.execute(sql)
    database.commit()

    # 向被关注人的粉丝表中写入数据
    sql = '''
          INSERT INTO %s_FANS (FANS_ID) VALUE('%s')
          ''' % (USER_ID, USERNAME)
    cur.execute(sql)
    database.commit()

    # d.关闭数据库连接
    database.close()
    print("[关注他人]已经关闭WHXD数据库连接！")

    return HttpResponse("OK")
# --------------------------已完成-分割线----------------------#

@csrf_exempt
def unfollow(request):
    # 15.某用户取消关注他人指令处理
    USERNAME = ''
    USER_ID = ''

    if request.method == 'POST':
        print("[指令]某用户取消关注他人")
        print("username:", request.POST.get('username'))
        print("USER_ID:", request.POST.get('user_id'))
        USERNAME = request.POST.get('username')
        USER_ID = request.POST.get('user_id')
    elif request.method == 'GET':
        print("[指令]某用户取消关注他人")
        print("username:", request.GET.get('username'))
        print("USER_ID:", request.GET.get('user_id'))
        USERNAME = request.GET.get('username')
        USER_ID = request.GET.get('user_id')

    # a.打开数据库连接
    database = pymysql.connect(host='localhost'
                               , user='root'
                               , password='000000'
                               , port=3306
                               , database='WHXD')

    # b.使用cursor()方法创建一个游标对象cur
    cur = database.cursor()
    print("[取消关注他人]已经连接到WHXD数据库！")

    # 从该用户的关注表中删除数据
    sql = '''
          DELETE FROM %s_FOLLOW WHERE FOLLOW_ID='%s'
          ''' % (USERNAME, USER_ID)
    cur.execute(sql)
    database.commit()

    # 判断他人粉丝表是否存在
    sql = '''
          SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME='%s_FANS' 
          ''' % USER_ID
    cur.execute(sql)
    result = cur.fetchall()
    if len(result) != 0:
        # 从被关注人的粉丝表中删除数据
        sql = '''
              DELETE FROM %s_FANS WHERE FANS_ID='%s'
              ''' % (USER_ID, USERNAME)
        cur.execute(sql)
        database.commit()

    # d.关闭数据库连接
    database.close()
    print("[取消关注他人]已经关闭WHXD数据库连接！")

    return HttpResponse("OK")
# --------------------------已完成-分割线----------------------#

@csrf_exempt
def collect(request):
    USERNAME = ''
    CONTENT_ID = ''
    TIME = ''
    # 16.某用户收藏内容指令处理
    # 请求领域数据
    if request.method == 'POST':
        print("[指令]某用户收藏内容")
        print("username:", request.POST.get('username'))
        print("contentid:", request.POST.get('contentid'))
        print("time:", request.POST.get('time'))
        USERNAME = request.POST.get('username')
        CONTENT_ID = request.POST.get('contentid')
        TIME = request.POST.get('time')
    elif request.method == 'GET':
        print("[指令]某用户收藏内容")
        print("username:", request.GET.get('username'))
        print("contentid:", request.GET.get('contentid'))
        print("time:", request.GET.get('time'))
        USERNAME = request.GET.get('username')
        CONTENT_ID = request.GET.get('contentid')
        TIME = request.GET.get('time')

    # a.打开数据库连接
    database = pymysql.connect(host='localhost'
                               , user='root'
                               , password='000000'
                               , port=3306
                               , database='WHXD')

    # b.使用cursor()方法创建一个游标对象cur
    cur = database.cursor()
    print("[收藏内容]已经连接到WHXD数据库！")

    sql = '''
          INSERT INTO COLLECT (TIME,CONTENT_ID,USER_ID) VALUE('%s',%s,'%s')
          ''' % (TIME, CONTENT_ID, USERNAME)
    cur.execute(sql)
    database.commit()
    # d.关闭数据库连接
    database.close()
    print("[收藏内容]已经关闭WHXD数据库连接！")

    return HttpResponse("OK")
# --------------------------已完成-分割线----------------------#

@csrf_exempt
def uncollect(request):
    USERNAME = ''
    CONTENT_ID = ''
    TIME = ''
    # 17.某用户取消收藏内容指令处理
    # 请求领域数据
    if request.method == 'POST':
        print("[指令]某用户取消收藏内容")
        print("username:", request.POST.get('username'))
        print("contentid:", request.POST.get('contentid'))
        print("time:", request.POST.get('time'))
        USERNAME = request.POST.get('username')
        CONTENT_ID = request.POST.get('contentid')
        TIME = request.POST.get('time')
    elif request.method == 'GET':
        print("[指令]某用户取消收藏内容")
        print("username:", request.GET.get('username'))
        print("contentid:", request.GET.get('contentid'))
        print("time:", request.GET.get('time'))
        USERNAME = request.GET.get('username')
        CONTENT_ID = request.GET.get('contentid')
        TIME = request.GET.get('time')

    # a.打开数据库连接
    database = pymysql.connect(host='localhost'
                               , user='root'
                               , password='000000'
                               , port=3306
                               , database='WHXD')

    # b.使用cursor()方法创建一个游标对象cur
    cur = database.cursor()
    print("[取消收藏内容]已经连接到WHXD数据库！")

    sql = '''
          DELETE FROM COLLECT WHERE CONTENT_ID=%s AND USER_ID = '%s'
          ''' % (CONTENT_ID, USERNAME)
    cur.execute(sql)
    database.commit()
    # d.关闭数据库连接
    database.close()
    print("[取消收藏内容]已经关闭WHXD数据库连接！")

    return HttpResponse("OK")
# --------------------------已完成-分割线----------------------#

@csrf_exempt
def agree(request):
    USERNAME = ''
    CONTENT_ID = ''
    TIME = ''
    # 20.某用户点赞内容指令处理
    # 请求领域数据
    if request.method == 'POST':
        print("[指令]某用户点赞内容")
        print("username:", request.POST.get('username'))
        print("contentid:", request.POST.get('contentid'))
        print("time:", request.POST.get('time'))
        USERNAME = request.POST.get('username')
        CONTENT_ID = request.POST.get('contentid')
        TIME = request.POST.get('time')
    elif request.method == 'GET':
        print("[指令]某用户点赞内容")
        print("username:", request.GET.get('username'))
        print("contentid:", request.GET.get('contentid'))
        print("time:", request.GET.get('time'))
        USERNAME = request.GET.get('username')
        CONTENT_ID = request.GET.get('contentid')
        TIME = request.GET.get('time')

    # a.打开数据库连接
    database = pymysql.connect(host='localhost'
                               , user='root'
                               , password='000000'
                               , port=3306
                               , database='WHXD')

    # b.使用cursor()方法创建一个游标对象cur
    cur = database.cursor()
    print("[点赞内容]已经连接到WHXD数据库！")

    sql = '''
          INSERT INTO AGREE (TIME,CONTENT_ID,USER_ID) VALUE('%s',%s,'%s')
          ''' % (TIME, CONTENT_ID, USERNAME)
    cur.execute(sql)
    database.commit()
    # d.关闭数据库连接
    database.close()
    print("[点赞内容]已经关闭WHXD数据库连接！")

    return HttpResponse("OK")
# --------------------------已完成-分割线----------------------#

@csrf_exempt
def unagree(request):
    USERNAME = ''
    CONTENT_ID = ''
    TIME = ''
    # 20.某用户点赞内容指令处理
    # 请求领域数据
    if request.method == 'POST':
        print("[指令]某用户取消点赞内容")
        print("username:", request.POST.get('username'))
        print("contentid:", request.POST.get('contentid'))
        print("time:", request.POST.get('time'))
        USERNAME = request.POST.get('username')
        CONTENT_ID = request.POST.get('contentid')
        TIME = request.POST.get('time')
    elif request.method == 'GET':
        print("[指令]某用户取消点赞内容")
        print("username:", request.GET.get('username'))
        print("contentid:", request.GET.get('contentid'))
        print("time:", request.GET.get('time'))
        USERNAME = request.GET.get('username')
        CONTENT_ID = request.GET.get('contentid')
        TIME = request.GET.get('time')

    # a.打开数据库连接
    database = pymysql.connect(host='localhost'
                               , user='root'
                               , password='000000'
                               , port=3306
                               , database='WHXD')

    # b.使用cursor()方法创建一个游标对象cur
    cur = database.cursor()
    print("[取消点赞内容]已经连接到WHXD数据库！")

    sql = '''
          DELETE FROM AGREE WHERE CONTENT_ID=%s AND USER_ID = '%s'
          ''' % (CONTENT_ID, USERNAME)
    cur.execute(sql)
    database.commit()
    # d.关闭数据库连接
    database.close()
    print("[取消点赞内容]已经关闭WHXD数据库连接！")

    return HttpResponse("OK")
# --------------------------已完成-分割线----------------------#

@csrf_exempt
def pushcontent(request):
    USERNAME = ''
    TYPE = ''
    FIELD_ID = ''
    DATE = ''
    DETAIL = ''
    TITLE = ''
    NODEONE = ''
    NODETWO = ''
    NODETHREE = ''

    # a.打开数据库连接
    database = pymysql.connect(host='localhost'
                               , user='root'
                               , password='000000'
                               , port=3306
                               , database='WHXD')
    # b.使用cursor()方法创建一个游标对象cur
    cur = database.cursor()
    print("[上传内容指令]已经连接到WHXD数据库！")

    # 22.某用户发布内容指令处理
    if request.method == 'POST':
        type = request.POST.get('type')
        if type == '0':
            print("[指令]某用户发布[动态]内容")
            print("username:", request.POST.get('username'))
            print("type: 动态")
            print("field:", request.POST.get('field'))
            print("time:", request.POST.get('time'))
            print("detail:", request.POST.get('detail'))
            USERNAME = request.POST.get('username')
            TYPE = "动态"
            FIELD_ID = request.POST.get('field')
            DATE = request.POST.get('time')
            DETAIL = request.POST.get('detail')
            DETAIL = DETAIL.replace("\n", "\\\\n")
            print(DETAIL)
            DETAIL = '{"detail":"' + DETAIL + '"' + '}'
            print(DETAIL)
        elif type == '1':
            print("[指令]某用户发布[项目]内容")
            print("username:", request.POST.get('username'))
            print("type: 项目")
            print("field:", request.POST.get('field'))
            print("time:", request.POST.get('time'))
            print('title:', request.POST.get('title'))
            print('nodeone', request.POST.get('nodeone'))
            print('nodetwo', request.POST.get('nodetwo'))
            print('nodethree', request.POST.get('nodethree'))
            print("detail:", request.POST.get('detail'))
            USERNAME = request.POST.get('username')
            TYPE = "项目"
            FIELD_ID = request.POST.get('field')
            DATE = request.POST.get('time')
            DETAIL = request.POST.get('detail')
            TITLE = request.POST.get('title')
            NODEONE = request.POST.get('nodeone')
            NODETWO = request.POST.get('nodetwo')
            NODETHREE = request.POST.get('nodethree')
            DETAIL = DETAIL.replace("\n", "\\\\n")
            DETAIL = '{"title":"' + TITLE + '"' + \
                     ',"nodeone":"' + NODEONE + '"' + \
                     ',"nodetwo":"' + NODETWO + '"' + \
                     ',"nodethree":"' + NODETHREE + '"' + \
                     ',"detail":"' + DETAIL + '"' + \
                     '}'
            print(DETAIL)
        elif type == '2':
            print("[指令]某用户发布[文章]内容")
            print("username:", request.POST.get('username'))
            print("type: 文章")
            print("field:", request.POST.get('field'))
            print("time:", request.POST.get('time'))
            print("detail:", request.POST.get('detail'))
            USERNAME = request.POST.get('username')
            TYPE = "文章"
            FIELD_ID = request.POST.get('field')
            DATE = request.POST.get('time')
            DETAIL = request.POST.get('detail')

            DETAIL = DETAIL.replace("\n", "\\\\n")
            print(DETAIL)
            DETAIL = '{"detail":"' + DETAIL + '"' + '}'
            print(DETAIL)
    elif request.method == 'GET':
        type = request.GET.get('type')
        if type == '0':
            print("[指令]某用户发布[动态]内容")
            print("username:", request.GET.get('username'))
            print("type: 动态")
            print("field:", request.GET.get('field'))
            print("time:", request.GET.get('time'))
            print("detail:", request.GET.get('detail'))
            USERNAME = request.GET.get('username')
            TYPE = "动态"
            FIELD_ID = request.GET.get('field')
            DATE = request.GET.get('time')
            DETAIL = request.GET.get('detail')

            DETAIL = DETAIL.replace("\n", "\\\\n")
            print(DETAIL)
            DETAIL = '{"detail":"' + DETAIL + '"' + '}'
            print(DETAIL)
        elif type == '1':
            print("[指令]某用户发布[项目]内容")
            print("username:", request.GET.get('username'))
            print("type: 项目")
            print("field:", request.GET.get('field'))
            print("time:", request.GET.get('time'))
            print('title:', request.GET.get('title'))
            print('nodeone', request.GET.get('nodeone'))
            print('nodetwo', request.GET.get('nodetwo'))
            print('nodethree', request.GET.get('nodethree'))
            print("detail:", request.GET.get('detail'))
            USERNAME = request.GET.get('username')
            TYPE = "项目"
            FIELD_ID = request.GET.get('field')
            DATE = request.GET.get('time')
            DETAIL = request.GET.get('detail')
            TITLE = request.GET.get('title')
            NODEONE = request.GET.get('nodeone')
            NODETWO = request.GET.get('nodetwo')
            NODETHREE = request.GET.get('nodethree')
            DETAIL = DETAIL.replace("\n", "\\\\n")
            DETAIL = '{"title":"' + TITLE + '"' + \
                     ',"nodeone":"' + NODEONE + '"' + \
                     ',"nodetwo":"' + NODETWO + '"' + \
                     ',"nodethree":"' + NODETHREE + '"' + \
                     ',"detail":"' + DETAIL + '"' + \
                     '}'
            print(DETAIL)
        elif type == '2':
            print("[指令]某用户发布[文章]内容")
            print("username:", request.GET.get('username'))
            print("type: 文章")
            print("field:", request.GET.get('field'))
            print("time:", request.GET.get('time'))
            print("detail:", request.GET.get('detail'))
            USERNAME = request.GET.get('username')
            TYPE = "文章"
            FIELD_ID = request.GET.get('field')
            DATE = request.GET.get('time')
            DETAIL = request.GET.get('detail')
            DETAIL = DETAIL.replace("\n", "\\\\n")
            print(DETAIL)
            DETAIL = '{"detail":"' + DETAIL + '"' + '}'
            print(DETAIL)
    content_insertsql = '''
                      INSERT INTO USER_CONTENTS (TYPE,DATE,READVALUE,DETAIL,USER_ID,FIELD_ID) VALUE('%s','%s',0,'%s','%s',%s)
                      ''' % (TYPE, DATE, DETAIL, USERNAME, FIELD_ID)
    # c.使用execute()方法执行SQL语句
    cur.execute(content_insertsql)
    database.commit()
    # d.关闭数据库连接
    database.close()
    print("[上传内容指令]已经关闭WHXD数据库连接！")
    return HttpResponse("OK")
# --------------------------已完成-分割线----------------------#

@csrf_exempt
def deleteusercontent(request):
    USERNAME = ''
    TYPE = ''
    CONTENTID = ''

    if request.method == 'POST':
        print("[指令]删除用户内容")
        USERNAME = request.POST.get('username')
        TYPE = request.POST.get('type')
        CONTENTID = request.POST.get('contentid')
    elif request.method == 'GET':
        print("[指令]删除用户内容")
        USERNAME = request.GET.get('username')
        TYPE = request.GET.get('type')
        CONTENTID = request.GET.get('contentid')

    # a.打开数据库连接
    database = pymysql.connect(host='localhost'
                               , user='root'
                               , password='000000'
                               , port=3306
                               , database='WHXD')

    # b.使用cursor()方法创建一个游标对象cur
    cur = database.cursor()
    print("[删除用户创作内容]已经连接到WHXD数据库！")
    sql = '''
          DELETE FROM USER_CONTENTS WHERE CONTENT_ID=%s AND USER_ID='%s'
          ''' % (CONTENTID, USERNAME)
    print(sql)
    cur.execute(sql)
    database.commit()
    # d.关闭数据库连接
    database.close()
    print("[删除用户创作内容]已经关闭WHXD数据库连接！")
    return HttpResponse('OK')
# --------------------------已完成-分割线----------------------#

@csrf_exempt
def message(request):
    USERNAME = ''
    MESSAGETYPE = ''
    # 请求领域数据
    if request.method == 'POST':
        print("[指令]消息列表")
        USERNAME = request.POST.get('username')
        MESSAGETYPE = request.POST.get('messagetype')
    elif request.method == 'GET':
        print("[指令]消息列表")
        USERNAME = request.GET.get('username')
        MESSAGETYPE = request.GET.get('messagetype')

    # a.打开数据库连接
    database = pymysql.connect(host='localhost'
                               , user='root'
                               , password='000000'
                               , port=3306
                               , database='WHXD')
    # b.使用cursor()方法创建一个游标对象cur
    cur = database.cursor()
    print("[请求消息列表]已经连接到WHXD数据库！")

    sql = ''
    if MESSAGETYPE == 'AGREE' or MESSAGETYPE == 'COLLECT':
        sql = '''
              SELECT %s.USER_ID,%s.CONTENT_ID FROM %s,USER_CONTENTS
              WHERE %s.CONTENT_ID=USER_CONTENTS.CONTENT_ID AND USER_CONTENTS.USER_ID = '%s'
              ''' % (MESSAGETYPE, MESSAGETYPE, MESSAGETYPE, MESSAGETYPE, USERNAME)
    else:
        sql = '''
              SELECT DISCUSS.USER_ID,DISCUSS.CONTENT_ID,DISCUSS.DETAIL FROM DISCUSS,USER_CONTENTS
              WHERE DISCUSS.CONTENT_ID=USER_CONTENTS.CONTENT_ID AND USER_CONTENTS.USER_ID = '%s'
              ''' % USERNAME
    print(sql)
    cur.execute(sql)
    result = cur.fetchall()
    # d.关闭数据库连接
    database.close()
    print("[请求领域列表]已经关闭WHXD数据库连接！")

    result_len = len(result)
    if result_len == 0:
        print("领域列表查询为空！")
        return HttpResponse("NULL")
    else:
        backjson = {}
        backjson['sum'] = result_len
        if MESSAGETYPE == 'AGREE':
            for i in range(0, result_len):
                record = result[i][0] + "点赞你内容：" + str(result[i][1])
                backjson[str(i)] = record
        elif MESSAGETYPE == 'COLLECT':
            for i in range(0, result_len):
                record = result[i][0] + "收藏你内容：" + str(result[i][1])
                backjson[str(i)] = record
        else:
            for i in range(0, result_len):
                record = result[i][0] + "评论你内容：" + str(result[i][1]) + ",评论：" + result[i][2]
                backjson[str(i)] = record

        print("backjson is:", backjson)
    return HttpResponse(json.dumps(backjson, ensure_ascii=False))
# --------------------------已完成-分割线----------------------#

@csrf_exempt
def usercontentlist(request):
    USERNAME = ''
    TYPE = ''
    # 7.某用户动态列表请求指令处理
    if request.method == 'POST':
        print("[指令]用户内容列表")
        USERNAME = request.POST.get('username')
        TYPE = request.POST.get('type')
    elif request.method == 'GET':
        print("[指令]用户内容列表")
        USERNAME = request.GET.get('username')
        TYPE = request.GET.get('type')

    # a.打开数据库连接
    database = pymysql.connect(host='localhost'
                               , user='root'
                               , password='000000'
                               , port=3306
                               , database='WHXD')

    if TYPE == 'dtlist':
        # b.使用cursor()方法创建一个游标对象cur
        cur = database.cursor()
        print("[请求用户动态列表]已经连接到WHXD数据库！")
        sql = '''
                SELECT CONTENT_ID,TYPE,DATE,READVALUE,DETAIL,USER_ID,FIELD_ID FROM USER_CONTENTS
                WHERE TYPE='动态' AND USER_ID='%s' ORDER BY DATE DESC
              ''' % USERNAME
        cur.execute(sql)
        result = cur.fetchall()
        # d.关闭数据库连接
        database.close()
        print("[请求用户动态列表]已经关闭WHXD数据库连接！")

        result_len = len(result)
        if result_len == 0:
            print("用户动态列表查询为空！")
            return HttpResponse("NULL")
        else:
            backjson = {}
            table_fiels = ['CONTENT_ID', 'TYPE', 'DATE', 'READVALUE', 'DETAIL', 'USER_ID', 'FIELD_ID']
            backjson['sum'] = result_len
            for i in range(0, result_len):
                record = {}
                for j in range(0, len(result[i])):
                    if j == 2:
                        record[table_fiels[j]] = result[i][j].strftime('%Y-%m-%d %H:%M:%S')
                    else:
                        record[table_fiels[j]] = result[i][j]
                backjson[str(i)] = record
            print("backjson is:", backjson)
        return HttpResponse(json.dumps(backjson, ensure_ascii=False))
    elif TYPE == 'xmlist':
        # a.打开数据库连接
        database = pymysql.connect(host='localhost'
                                   , user='root'
                                   , password='000000'
                                   , port=3306
                                   , database='WHXD')

        # b.使用cursor()方法创建一个游标对象cur
        cur = database.cursor()
        print("[用户项目列表]已经连接到WHXD数据库！")
        sql = '''
                SELECT CONTENT_ID,TYPE,DATE,READVALUE,DETAIL,USER_ID,FIELD_ID FROM USER_CONTENTS
                WHERE TYPE='项目' AND USER_ID='%s' ORDER BY DATE DESC
              ''' % USERNAME
        cur.execute(sql)
        result = cur.fetchall()
        # d.关闭数据库连接
        database.close()
        print("[用户项目列表]已经关闭WHXD数据库连接！")

        result_len = len(result)
        if result_len == 0:
            print("用户项目列表查询为空！")
            return HttpResponse("NULL")
        else:
            backjson = {}
            table_fiels = ['CONTENT_ID', 'TYPE', 'DATE', 'READVALUE', 'DETAIL', 'USER_ID', 'FIELD_ID']
            backjson['sum'] = result_len
            for i in range(0, result_len):
                record = {}
                for j in range(0, len(result[i])):
                    if j == 2:
                        record[table_fiels[j]] = result[i][j].strftime('%Y-%m-%d %H:%M:%S')
                    else:
                        record[table_fiels[j]] = result[i][j]
                backjson[str(i)] = record
            print("backjson is:", backjson)

        return HttpResponse(json.dumps(backjson, ensure_ascii=False))
    elif TYPE == 'wzlist':
        # a.打开数据库连接
        database = pymysql.connect(host='localhost'
                                   , user='root'
                                   , password='000000'
                                   , port=3306
                                   , database='WHXD')

        # b.使用cursor()方法创建一个游标对象cur
        cur = database.cursor()
        print("[用户文章列表]已经连接到WHXD数据库！")
        sql = '''
                SELECT CONTENT_ID,TYPE,DATE,READVALUE,DETAIL,USER_ID,FIELD_ID FROM USER_CONTENTS
                WHERE TYPE='文章' AND USER_ID='%s' ORDER BY DATE DESC
              ''' % USERNAME
        cur.execute(sql)
        result = cur.fetchall()
        # d.关闭数据库连接
        database.close()
        print("[用户文章列表]已经关闭WHXD数据库连接！")

        result_len = len(result)
        if result_len == 0:
            print("用户文章列表查询为空！")
            return HttpResponse("NULL")
        else:
            backjson = {}
            table_fiels = ['CONTENT_ID', 'TYPE', 'DATE', 'READVALUE', 'DETAIL', 'USER_ID', 'FIELD_ID']
            backjson['sum'] = result_len
            for i in range(0, result_len):
                record = {}
                for j in range(0, len(result[i])):
                    if j == 2:
                        record[table_fiels[j]] = result[i][j].strftime('%Y-%m-%d %H:%M:%S')
                    else:
                        record[table_fiels[j]] = result[i][j]
                backjson[str(i)] = record
            print("backjson is:", backjson)
        return HttpResponse(json.dumps(backjson, ensure_ascii=False))
    elif TYPE == 'collectlist':
        # a.打开数据库连接
        database = pymysql.connect(host='localhost'
                                   , user='root'
                                   , password='000000'
                                   , port=3306
                                   , database='WHXD')

        # b.使用cursor()方法创建一个游标对象cur
        cur = database.cursor()
        print("[用户收藏列表]已经连接到WHXD数据库！")

        # 从收藏列表中找到USERNAME的收藏记录的CONTENT_ID
        sql = '''
              SELECT CONTENT_ID FROM COLLECT WHERE USER_ID='%s'
              ''' % USERNAME
        print(sql)
        cur.execute(sql)
        result = cur.fetchall()
        result_len = len(result)
        finallresult = []

        for i in range(0, result_len):
            # 从USER_CONTENTS表里查出改CONTENT_ID的详细信息
            tsql = '''
                  SELECT CONTENT_ID,TYPE,DATE,READVALUE,DETAIL,USER_ID,FIELD_ID FROM USER_CONTENTS
                  WHERE CONTENT_ID=%s
                  ''' % result[i][0]
            print(tsql)
            cur.execute(tsql)
            tresult = cur.fetchall()
            finallresult.append(tresult[0])

        # d.关闭数据库连接
        database.close()
        print("[用户收藏列表]已经关闭WHXD数据库连接！")

        finallresult_len = len(finallresult)
        if finallresult_len == 0:
            print("用户收藏列表查询为空！")
            return HttpResponse("NULL")
        else:
            backjson = {}
            table_fiels = ['CONTENT_ID', 'TYPE', 'DATE', 'READVALUE', 'DETAIL', 'USER_ID', 'FIELD_ID']
            backjson['sum'] = finallresult_len
            for i in range(0, finallresult_len):
                record = {}
                for j in range(0, len(finallresult[i])):
                    if j == 2:
                        record[table_fiels[j]] = finallresult[i][j].strftime('%Y-%m-%d %H:%M:%S')
                    else:
                        record[table_fiels[j]] = finallresult[i][j]
                backjson[str(i)] = record
            print("backjson is:", backjson)
        return HttpResponse(json.dumps(backjson, ensure_ascii=False))
# --------------------------已完成-分割线----------------------#

@csrf_exempt
def pulldiscuss(request):
    CONTENTID = ''
    # 请求领域数据
    if request.method == 'POST':
        print("[指令]评论列表")
        CONTENTID = request.POST.get('contentid')
    elif request.method == 'GET':
        print("[指令]评论列表")
        CONTENTID = request.GET.get('contentid')

    # a.打开数据库连接
    database = pymysql.connect(host='localhost'
                               , user='root'
                               , password='000000'
                               , port=3306
                               , database='WHXD')
    # b.使用cursor()方法创建一个游标对象cur
    cur = database.cursor()
    print("[请求评论列表]已经连接到WHXD数据库！")

    sql = '''SELECT TIME,DETAIL,USER_ID FROM DISCUSS WHERE CONTENT_ID=%s ORDER BY TIME DESC'''%CONTENTID
    cur.execute(sql)
    result = cur.fetchall()
    # d.关闭数据库连接
    database.close()
    print("[请求评论列表]已经关闭WHXD数据库连接！")

    result_len = len(result)
    if result_len == 0:
        print("内容评论列表查询为空！")
        return HttpResponse("NULL")
    else:
        backjson = {}
        backjson['sum'] = result_len
        for i in range(0, result_len):
            record = {}
            record['detail'] = result[i][0].strftime('%Y-%m-%d %H:%M:%S')+"\n"+result[i][1]+"\n"+result[i][2]
            backjson[str(i)] = record
        print("backjson is:", backjson)
    return HttpResponse(json.dumps(backjson, ensure_ascii=False))
# --------------------------已完成-分割线----------------------#

@csrf_exempt
def pushdiscuss(request):
    USERNAME = ''
    CONTENTID = ''
    DETAIL = ''
    TIME = ''
    # 请求领域数据
    if request.method == 'POST':
        print("[指令]发布评论")
        USERNAME = request.POST.get('username')
        CONTENTID = request.POST.get('contentid')
        DETAIL = request.POST.get('detail')
        TIME = request.POST.get('time')
    elif request.method == 'GET':
        print("[指令]发布评论")
        USERNAME = request.GET.get('username')
        CONTENTID = request.GET.get('contentid')
        DETAIL = request.GET.get('detail')
        TIME = request.GET.get('time')

    # a.打开数据库连接
    database = pymysql.connect(host='localhost'
                               , user='root'
                               , password='000000'
                               , port=3306
                               , database='WHXD')
    # b.使用cursor()方法创建一个游标对象cur
    cur = database.cursor()
    print("[发布评论]已经连接到WHXD数据库！")

    sql = '''
          INSERT INTO DISCUSS (TIME,DETAIL,CONTENT_ID,USER_ID) VALUE('%s','%s',%s,'%s')
          '''%(TIME,DETAIL,CONTENTID,USERNAME)
    print(sql)
    cur.execute(sql)
    database.commit()
    # d.关闭数据库连接
    database.close()
    print("[发布评论]已经关闭WHXD数据库连接！")

    return HttpResponse("OK")
# --------------------------已完成-分割线----------------------#

@csrf_exempt
def count(request):
    USERNAME = ''

    # 请求领域数据
    if request.method == 'POST':
        print("[指令]发布评论")
        USERNAME = request.POST.get('username')
    elif request.method == 'GET':
        print("[指令]发布评论")
        USERNAME = request.GET.get('username')

    # a.打开数据库连接
    database = pymysql.connect(host='localhost'
                               , user='root'
                               , password='000000'
                               , port=3306
                               , database='WHXD')
    # b.使用cursor()方法创建一个游标对象cur
    cur = database.cursor()
    print("[请求计数]已经连接到WHXD数据库！")

    sum =[]
    sql = ''
    result = ''

    sql = '''SELECT COUNT(*) FROM %s_FOLLOW'''%USERNAME
    cur.execute(sql)
    result = cur.fetchall()
    result = str(result[0][0])
    print('sum:',result)
    sum.append(result)

    sql = '''SELECT COUNT(*) FROM %s_FANS'''%USERNAME
    cur.execute(sql)
    result = cur.fetchall()
    result = str(result[0][0])
    print('sum:',result)
    sum.append(result)

    sql = '''
          SELECT READVALUE FROM USER_CONTENTS WHERE USER_ID='%s'
          '''%USERNAME
    cur.execute(sql)
    result = cur.fetchall()
    tsum = 0
    for i in range(0,len(result)):
        tsum = tsum + int(result[i][0])
    result = str(tsum)
    print('sum:',result)
    sum.append(result)
    # d.关闭数据库连接
    database.close()
    print("[请求计数]已经关闭WHXD数据库连接！")

    return HttpResponse('OK:'+sum[0]+','+sum[1]+','+sum[2])
# --------------------------已完成-分割线----------------------#

@csrf_exempt
def searchuser(request):
    USERNAME = ''
    OBJECTNAME = ''
    # 请求领域数据
    if request.method == 'POST':
        print("[指令]搜索用户")
        USERNAME = request.POST.get('username')
        OBJECTNAME = request.POST.get('objectname')
    elif request.method == 'GET':
        print("[指令]搜索用户")
        USERNAME = request.GET.get('username')
        OBJECTNAME = request.GET.get('objectname')
    # a.打开数据库连接
    database = pymysql.connect(host='localhost'
                               , user='root'
                               , password='000000'
                               , port=3306
                               , database='WHXD')
    # b.使用cursor()方法创建一个游标对象cur
    cur = database.cursor()
    print("[搜索用户]已经连接到WHXD数据库！")

    sql = ''
    result = ''

    sql = '''SELECT USER_ID FROM USERS WHERE USER_ID='%s'
          '''%OBJECTNAME
    print(sql)
    cur.execute(sql)
    result = cur.fetchall()
    if len(result) == 0:
        # d.关闭数据库连接
        database.close()
        print("[搜索用户]已经关闭WHXD数据库连接！")
        return HttpResponse('NULL')
    else:
        sql = '''
              SELECT FOLLOW_ID FROM %s_FOLLOW WHERE FOLLOW_ID='%s'
              '''%(USERNAME.upper(),OBJECTNAME)
        cur.execute(sql)
        result = cur.fetchall()
        print(sql)
        # d.关闭数据库连接
        database.close()
        print("[搜索用户]已经关闭WHXD数据库连接！")

        if len(result) == 0:
            return HttpResponse('OK:'+OBJECTNAME+'[关注]')
        else:
            return HttpResponse('OK:' + OBJECTNAME + '[已关注]')
# --------------------------已完成-分割线----------------------#

@csrf_exempt
def followlist(request):
    USERNAME = ''
    # 请求领域数据
    if request.method == 'POST':
        print("[指令]用户关注列表请求")
        USERNAME = request.POST.get('username')
    elif request.method == 'GET':
        print("[指令]用户关注列表请求")
        USERNAME = request.GET.get('username')

    # a.打开数据库连接
    database = pymysql.connect(host='localhost'
                               , user='root'
                               , password='000000'
                               , port=3306
                               , database='WHXD')
    # b.使用cursor()方法创建一个游标对象cur
    cur = database.cursor()
    print("[关注列表请求]已经连接到WHXD数据库！")

    sql = '''SELECT FOLLOW_ID FROM %s_FOLLOW'''%USERNAME
    cur.execute(sql)
    result = cur.fetchall()
    # d.关闭数据库连接
    database.close()
    print("[关注列表请求]已经关闭WHXD数据库连接！")

    result_len = len(result)
    if result_len == 0:
        return HttpResponse("NULL")
    else:
        backjson = {}
        backjson['sum'] = result_len
        for i in range(0,result_len):
            backjson[str(i)] = result[i][0]+'[已关注]'
        print("backjson is:", backjson)
        return HttpResponse(json.dumps(backjson, ensure_ascii=False))
# --------------------------已完成-分割线----------------------#


@csrf_exempt
def fanslist(request):
    USERNAME = ''
    # 请求领域数据
    if request.method == 'POST':
        print("[指令]用户粉丝列表请求")
        USERNAME = request.POST.get('username')
    elif request.method == 'GET':
        print("[指令]用户粉丝列表请求")
        USERNAME = request.GET.get('username')

    # a.打开数据库连接
    database = pymysql.connect(host='localhost'
                               , user='root'
                               , password='000000'
                               , port=3306
                               , database='WHXD')
    # b.使用cursor()方法创建一个游标对象cur
    cur = database.cursor()
    print("[粉丝列表请求]已经连接到WHXD数据库！")

    sql = '''SELECT FANS_ID FROM %s_FANS'''%USERNAME
    cur.execute(sql)
    result = cur.fetchall()
    # d.关闭数据库连接
    database.close()
    print("[粉丝列表请求]已经关闭WHXD数据库连接！")

    result_len = len(result)
    if result_len == 0:
        return HttpResponse("NULL")
    else:
        backjson = {}
        backjson['sum'] = result_len
        for i in range(0,result_len):
            backjson[str(i)] = result[i][0]
        print("backjson is:", backjson)
        return HttpResponse(json.dumps(backjson, ensure_ascii=False))
# --------------------------已完成-分割线----------------------#





def index(request):
    # 测试页面
    return HttpResponse("OK")
